package com.danger.Model.action;

import android.content.Context;
import android.text.TextUtils;

import com.danger.Model.bean.UserInfoBean;
import com.danger.Model.listener.OnCommonListener;
import com.danger.Util.CheckCode;

import java.io.File;

import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;

/**
 * Created by danger on 2016/4/25.
 */
public class UserInfoModelImpl implements UserInfoModel {

    private Context context;

    public UserInfoModelImpl(Context context) {
        this.context = context;
    }

    @Override
    public void modifyUsername(String username, String id, final OnCommonListener listener) {
        UserInfoBean userInfo = new UserInfoBean();
        if (!TextUtils.isEmpty(username.trim())) {
            userInfo.setUsername(username);
            userInfo.update(context, id, new UpdateListener() {
                @Override
                public void onSuccess() {
                    listener.onSuccess("更新用户名成功");
                }

                @Override
                public void onFailure(int i, String s) {
                    listener.onError("更新用户名失败:" + s);
                }
            });
        } else {
            listener.onError("新用户名为空");
        }
    }

    @Override
    public void modifyPassword(String password, String confirm_password, String id, final OnCommonListener listener) {
        UserInfoBean userInfo = new UserInfoBean();
        if (TextUtils.isEmpty(password)) {
            listener.onError("密码为空");
        } else if (TextUtils.isEmpty(confirm_password)) {
            listener.onError("确认密码为空");
        } else if (!password.equals(confirm_password)) {
            listener.onError("前后输入的密码不一致");
        } else {
            userInfo.setPassword(password);
            userInfo.update(context, id, new UpdateListener() {
                @Override
                public void onSuccess() {
                    listener.onSuccess("更新密码成功");
                }

                @Override
                public void onFailure(int i, String s) {
                    listener.onError("更新密码失败:" + s);
                }
            });
        }
    }

    @Override
    public void modifyPhone(String phone, String id, final OnCommonListener listener) {
        UserInfoBean userInfo = new UserInfoBean();
        if (!TextUtils.isEmpty(phone)) {
            listener.onError("电话号码为空");
        } else {
            userInfo.setMobilePhoneNumber(phone);
            userInfo.update(context, id, new UpdateListener() {
                @Override
                public void onSuccess() {
                    listener.onSuccess("更新电话成功");
                }

                @Override
                public void onFailure(int i, String s) {
                    listener.onError("更新电话失败:" + s);
                }
            });
        }
    }

    @Override
    public void modifyEmail(String email, String id, final OnCommonListener listener) {
        UserInfoBean userInfo = new UserInfoBean();
        if (TextUtils.isEmpty(email)) {
            listener.onError("邮箱地址为空");
        } else {
            userInfo.setEmail(email);
            userInfo.update(context, id, new UpdateListener() {
                @Override
                public void onSuccess() {
                    listener.onSuccess("更新邮箱地址成功");
                }

                @Override
                public void onFailure(int i, String s) {
                    listener.onError("更新邮箱地址失败:" + s);
                }
            });
        }
    }

    @Override
    public void modifyPhoto(String url, String id, final OnCommonListener listener) {
        UserInfoBean userInfo = new UserInfoBean();
        if (TextUtils.isEmpty(url)) {
            listener.onError("未选择图片");
        } else {
            userInfo.setPhoto(url);
            userInfo.update(context, id, new UpdateListener() {
                @Override
                public void onSuccess() {
                    listener.onSuccess("更新头像成功");
                }

                @Override
                public void onFailure(int i, String s) {
                    listener.onError("更新头像失败:" + s);
                }
            });
        }
    }

    @Override
    public void upload(String path, final OnCommonListener listener) {
        final BmobFile bmobFile = new BmobFile(new File(path));
        bmobFile.uploadblock(context, new UploadFileListener() {
            @Override
            public void onSuccess() {
                listener.onSuccess(bmobFile.getFileUrl(context));
            }

            @Override
            public void onFailure(int i, String s) {
                listener.onError(CheckCode.checkByCode(i));
            }
        });
    }
}
